''' 鼠标操作与响应 '''
from turtle import down
import cv2 as cv

down_x, down_y = None, None
img = cv.imread('../resources/miku.jpg')

def onMouse(event, x, y, flags, param):
    ''' event:  0 -> 不点击鼠标(flag = 0)
                1 -> 左键点击  (flag = 1)
                2 -> 右键点击  (flag = 2)
                3 -> 中键点击  (flag = 3)
                4 -> 左键松开  (flag = 1)
                5 -> 右键松开  (flag = 2)
                10 -> 鼠标滚动 (flag = 0) (x=0, y=滚动的速度)

        x, y: 鼠标在图像上的坐标

        <del>实在不清楚就网上搜吧</del>
    '''
    global down_x, down_y, up_x, up_y, img

    if event == cv.EVENT_LBUTTONDOWN:
        down_x, down_y = x, y

    elif event == cv.EVENT_MOUSEMOVE:
        if down_x is None or down_y is None:
            return
        img_cp = img.copy()
        cv.rectangle(img_cp, (down_x, down_y), (x, y), (0, 0, 255), 2)
        cv.imshow('image', img_cp)

    elif event == cv.EVENT_LBUTTONUP:
        img_cp = img.copy()
        cv.rectangle(img, (down_x, down_y), (x, y), (0, 0, 255), 2)
        down_x, down_y, up_x, up_y = None, None, None, None

        # cv.imshow('image', img)
        cv.imshow('image', img_cp)

cv.imshow('image', img)
cv.setMouseCallback('image', onMouse)

while True:
    if cv.waitKey(0) == ord('q'):
        break

cv.destroyAllWindows()